package com.brds.netty.io;

import io.netty.buffer.ByteBuf;
import io.netty.channel.*;
import io.netty.util.CharsetUtil;
import io.netty.util.ReferenceCountUtil;

@ChannelHandler.Sharable
public class DiscardOutboundHandler extends ChannelOutboundHandlerAdapter {

    @Override
    public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) {
        ByteBuf in = (ByteBuf)msg;
        System.out.println("outbound msg: " + in.toString(CharsetUtil.UTF_8));

        // 如果不打算使用 super.write 对该消息进行传递，尽量应释放资源，并告知ChannelPromise相关信息已经被处理
        // 否则将抛出异常
        ReferenceCountUtil.release(msg);
        promise.setSuccess();
    }
}
